package com.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.constants.SystemConstants;
import com.feng.domain.ResponseResult;
import com.feng.domain.dto.AddLinkDto;
import com.feng.domain.dto.UpdateLinkDto;
import com.feng.domain.vo.AdminLinkVo;
import com.feng.domain.vo.ArticleListPageVo;
import com.feng.domain.vo.LinkVo;
import com.feng.domain.vo.UpdateLinkVo;
import com.feng.mapper.LinkMapper;
import com.feng.domain.entity.Link;
import com.feng.service.LinkService;
import com.feng.utils.BeanCopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2023-04-14 15:40:46
 */
@Service()
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public ResponseResult getAllLink() {
        //查询所有已发布的
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);

        List<Link> links = list(queryWrapper);

        //转化vo
        List<LinkVo> linkVos = BeanCopyUtil.articleListVo(links, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult getList(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), Link::getName, name);
        wrapper.eq(StringUtils.hasText(status), Link::getStatus, status);
        Page<Link> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, wrapper);
        List<AdminLinkVo> adminLinkVos = BeanCopyUtil.articleListVo(page.getRecords(), AdminLinkVo.class);
        return ResponseResult.okResult(new ArticleListPageVo(adminLinkVos, page.getTotal()));
    }

    @Override
    public ResponseResult addLink(AddLinkDto linkDto) {
        Link link = BeanCopyUtil.articleVo(linkDto, Link.class);
        save(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateGetList(Long id) {
        Link link = getById(id);
        UpdateLinkVo updateLinkVo = BeanCopyUtil.articleVo(link, UpdateLinkVo.class);
        return ResponseResult.okResult(updateLinkVo);
    }

    @Override
    public ResponseResult updateLink(UpdateLinkDto linkDto) {
        Link link = BeanCopyUtil.articleVo(linkDto, Link.class);
        updateById(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delLink(List<Long> ids) {
        removeBatchByIds(ids);
        return ResponseResult.okResult();
    }
}

